//
//  BlinkView.m
//  DecentProject
//
//  Created by shaowei on 13-4-14.
//  Copyright (c) 2013年 vin. All rights reserved.
//

#import "BlinkView.h"

@interface BlinkView()

@property (nonatomic,retain) NSTimer *timer;

@end

@implementation BlinkView

- (void)dealloc
{
    [_timer release];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        [self allInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self allInit];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self allInit];
    
}

/**
 初始化所有设置
 */
- (void)allInit{
    self.backgroundColor = [UIColor clearColor];
    [self startBlink];
}

#pragma mark - Function - Public

/**
 View创建后 自动开启闪烁
 */
- (void)startBlink{
    if (_timer) {
        if (![_timer isValid]) {
            [_timer fire];
        }
    }else{
        [self setBlinkInterval:_blinkInterval];
    }
    
}

/**
 在使用完毕后，要调用此方法，否则会有内存无法释放
 */
- (void)stopBlink{
    [_timer invalidate];
}

/**
 设置闪烁的时间间隔，默认为0.05s
 */
- (void)setBlinkInterval:(CGFloat)interval{
    _blinkInterval = interval;
    if (_timer) {
        [_timer invalidate];
    }
    
    //创建timer
    self.timer =[NSTimer scheduledTimerWithTimeInterval:interval
                                                 target:self
                                               selector:@selector(setNeedsDisplay)
                                               userInfo:nil
                                                repeats:YES];
}

#pragma mark - Graphic

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self creatCircleGradient];
}

static CGFloat interval = 0.03f;
static CGFloat startValue = 0.0f;

- (void)creatCircleGradient{
    
    startValue += interval;
    if (startValue>=1.0f || startValue< 0.01f) {
        interval = -interval;
    }
//    DLog(@"SW - startValue:%f",startValue);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    UIColor *startColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:startValue];
    CGFloat *startComponent = (CGFloat *)CGColorGetComponents(startColor.CGColor);
    
    UIColor *endColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.01f];
    CGFloat *endComponent = (CGFloat *)CGColorGetComponents(endColor.CGColor);
    
    CGFloat component[8] =
    {
        startComponent[0],
        startComponent[1],
        startComponent[2],
        startComponent[3],
        
        endComponent[0],
        endComponent[1],
        endComponent[2],
        endComponent[3],
    };
    
    CGFloat location[2] = { 0.0f,0.5f};
    
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, (const CGFloat *)&component, (const CGFloat*)&location, 2);
    
    
    CGColorSpaceRelease(colorSpace);
    
    CGPoint center = CGPointMake(self.frame.size.width*0.5f,
                                 self.frame.size.height*0.5f);
    CGContextDrawRadialGradient(context,
                                gradient,
                                center,
                                0,
                                center,
                                self.frame.size.width,
                                0);
    
    
    CGGradientRelease(gradient);
    
}



@end
